<?php
/**
 * 微信支付控制类
 */
namespace Wap\Controller;
class WxPayController extends CommonController{
    /**
     * 订单回调处理
     */
    public function notify_url(){
    	//去掉TP的GET参数，否则可能影响计算验证结果
		unset($_GET['_URL_'], $_POST['_URL_'], $_REQUEST['_URL_']);
       // 支付配置
		$pay_config = D('Config')->get('', 'pay');
		$appid            = $pay_config['weixin_web_appid'];
		$mchid            = $pay_config['weixin_web_mchid'];
		$app_key          = $pay_config['weixin_web_key'];
		$appsecret        = $pay_config['weixin_web_appsecret'];
		$cert_path = D('File')->get_file_info($pay_config['weixin_cert_file'], 'filepath');
		$key_path  = D('File')->get_file_info($pay_config['weixin_key_file'], 'filepath');

		import("WeixinPay",'Public/Class/Pay/WxPay');
		$weixin_pay = new \WeixinPay($appid, $mchid, $app_key, $appsecret, $cert_path, $key_path);

		$this->log_path = $weixin_pay->logs_path.'weixin_wap_'.date('Y_m_d').'.log';

		$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
		$this->logs('[异步]微信==>POST数据：'.print_r($_POST, true));
		$this->logs('[异步]微信==>GET数据：'.print_r($_GET, true));
		$this->logs('[异步]微信==>RAW_POST数据：'.$xml);
		//使用通用通知接口
		$notify = new \Notify_pub();
		$notify->saveData($xml);
		$post_data = $notify->data;
		$this->logs('[异步]微信==>RAW_POST数据：' . print_r($post_data, true));

		if($notify->checkSign() == TRUE){
			if ($post_data["return_code"] == "SUCCESS") {
				$this->logs('[异步]微信==>验证成功');
				//订单号
				$order_sn     = $post_data['out_trade_no'];
				//微信支付订单号
				$trade_no     = $post_data['transaction_id'];
				//支付金额
				$total_fee    = $post_data['total_fee'] / 100;
				//买方支付账号
				$buyer_email  = $post_data['openid'];
				//买方ID
				$buyer_id     = $post_data['openid'];
				//卖方账号
				$seller_email = $post_data['appid'];

				$this->logs('[异步]验证订单是否可以支付');
				$where['order_sn'] = $order_sn;
				$order = D('Order')->where($where)->find();
				if(!$order){
					$this->logs('[异步]此订单号不存在！');
					$this->return_xml('FAIL', '订单号不存在');
				}
				if($order['status'] != 'wait_pay'){
					$this->logs('[异步]此订单状态不能进行支付，订单状态：' . $order['status'] . '可支付状态：wait_pay');
					$this->return_xml('FAIL', '订单状态不对');
				}
				$this->logs('[异步]提交支付成功订单......');
				//提交订单支付
				D('Order')->order_pay('weixin',
									  $order_sn,
									  $trade_no,
									  $total_fee,
									  $buyer_email,
									  $buyer_id,
									  $seller_email);

				$this->logs('[异步]微信==>支付成功！');
				$this->return_xml('SUCCESS');
			}else{
				$this->logs('[异步]微信==>验证失败');
				$this->return_xml('FAIL', '验证失败');
			}
		}else{
		    //签名失败
		    $this->logs('[异步]签名失败');
		    $this->return_xml('FAIL', '签名失败');
		}
    }
	//写入日志
	protected function logs($content){
		\Think\Log::write($content, \Think\Log::INFO, '', $this->log_path);
	}
	/**
	 * 返回给微信信息
	 * @param  string $status 处理状态：成功SUCCESS，失败FAIL
	 * @param  string $msg    处理结果：如OK
	 */
	public function return_xml($status='SUCCESS', $msg='OK'){
		$xml = '<xml><return_code><![CDATA[' . $status . ']]></return_code><return_msg><![CDATA[' . $msg . ']]></return_msg></xml>';
		exit($xml);
	}
}
?>